var mongoose=require("mongoose");
// var ObjectID=require('mongodb').ObjectID;  
mongoose.Promise = global.Promise; 
var movieSchema=new mongoose.Schema({
	doctor:String,
	title:String,
	language:String,
	country:String,
	poster:String,
	year:Number,
	meta:{
		createAt:{
			type:Date,
			default:Date.now()
		},
		updateAt:{
			type:Date,
			default:Date.now()
		}
	}
},{
	collection:"nodeWeb"
});

//在每次保存之前执行
movieSchema.pre("save",function(next){
	//如果数据是新的
	if(this.isNew){
		this.meta.createAt=this.meta.updateAt=Date.now();
	}else{
		this.meta.updateAt=Date.now();
	}
	//继续执行之后的存储步骤
	next();
});

//创建静态方法
movieSchema.statics={
	fetch:function(callback){
		this.find({},callback);
	},
	findOne:function(num,callback){
		console.log(num)
		this.find({_id:mongoose.mongo.ObjectId(num)},callback);
	}
}

module.exports=movieSchema;